import React from 'react';
import {history} from 'umi'
import { Button, Checkbox, Form, type FormProps, Input } from 'antd';
import style from './login.css'
import { subLogin } from '@/utils/api';

// import router from 'um';

// 普通跳转，不带参数

type FieldType = {
  username?: string;
  password?: string;
  remember?: string;
};
const subLoginEvent = async (params:any) => {
    const res = await subLogin(params);
        if(res.code == 200){
          localStorage.setItem('token',res.data.token);
          history.push('/index');
        }
} 

const onFinish: FormProps<FieldType>["onFinish"] = (values) => {
    subLoginEvent(values)
};

const onFinishFailed: FormProps<FieldType>["onFinishFailed"] = (errorInfo) => {
  console.log('Failed:', errorInfo);
};

const Login: React.FC = () => (
  <div className={style.loginPage}>
  <div className={style.loginForm}>
      <div className={style.loginLogo}> </div>
      <div>
        <Form
          name="basic"
          labelCol={{ span: 5 }}
          wrapperCol={{ span: 19 }}
          style={{ maxWidth: 600 }}
          initialValues={{ remember: true }}
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
          autoComplete="off"
        >
          <Form.Item<FieldType>
            label="账号"
            name="username"
            rules={[{ required: true, message: 'Please input your username!' }]}
          >
            <Input />
          </Form.Item>

          <Form.Item<FieldType>
            label="密码"
            name="password"
            rules={[{ required: true, message: 'Please input your password!' }]}
          >
            <Input.Password />
          </Form.Item>

          <Form.Item<FieldType>
            name="remember"
            valuePropName="checked"
            wrapperCol={{ offset: 5, span: 19 }}
          >
            <Checkbox>记住密码</Checkbox>
          </Form.Item>

          <Form.Item wrapperCol={{ offset: 5, span: 19 }}>
            <Button type="primary" htmlType="submit">
              登录
            </Button>
          </Form.Item>
        </Form>
      </div>
    </div>
  </div>
);

export default Login;